﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.Event.EditEvent
{
    public partial class EditEventUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                // load for sender
                if (Request.QueryString["EventId"] != null)
                {
                    int eventid = Int32.Parse(Request.QueryString["EventId"]);


                    using (EventProxy proxy = new EventProxy())
                    {
                        EventDataset.T_EventDataTable eventDT = proxy.GetEventDetailById(eventid);
                        if (SPContext.Current.Web.CurrentUser.Name == eventDT[0].UserName)
                        {
                            Session["EventDT"] = eventDT;
                            this.txtSubject.Text = eventDT[0].Subject;
                            this.txtSummary.Text = eventDT[0].EventContent;
                            this.dtEndDateFrom.SelectedDate = eventDT[0].EndDate;
                            string status = eventDT[0].Status;
                            if (status == "未完成")
                            {
                                this.rblStatus.Items[1].Selected = true;
                                this.rblStatus.Items[0].Selected = false;
                            }
                            else
                            {
                                this.rblStatus.Items[0].Selected = true;
                                this.rblStatus.Items[1].Selected = false;
                            }
                        }
                        else
                        {
                            Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["JCYUrl"].ToString());
                        }
                    }
                  
                }

             }
        }
      

        protected void imgSavePublish_Click(object sender, ImageClickEventArgs e)
        {
            using (EventProxy proxy = new EventProxy())
            {

                // update for event sender

                if (Session["EventDT"] != null)
                {
                    var eventDT = (EventDataset.T_EventDataTable)Session["EventDT"];
                    var editEventRow = eventDT[0];
                    editEventRow.Subject = this.txtSubject.Text;
                    editEventRow.EventContent = this.txtSummary.Text;
                    editEventRow.EndDate = this.dtEndDateFrom.SelectedDate;
                    editEventRow.Status = this.rblStatus.SelectedValue;
                    JCYUtility.UpdateCommonFields(editEventRow, "admin");
                    proxy.UpdateEvent(eventDT);
                    Response.Redirect("SearchEventSender.aspx");
                }

            }
      
        }

     }
    
}
